package com.example.hdwx.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.hdwx.myapplication.base_activity.Login_Activity;
import com.example.hdwx.myapplication.base_activity.SetUp_Activity;
import com.example.hdwx.myapplication.eneity.goodChoice_eneitys.CyclicGraphEneity;
import com.example.hdwx.myapplication.eneity.goodChoice_eneitys.ImagUrlEneity;
import com.example.hdwx.myapplication.eneity.goodChoice_eneitys.LargeFragmentsEneity;
import com.example.hdwx.myapplication.untils.UniversalImage;
import com.example.hdwx.myapplication.R;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.List;

/**
 * 优选碎片的适配器
 */
public class Goodchoice_Fragment_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context context;
    ImageView imageView;
    List<CyclicGraphEneity> list;
    List<ImagUrlEneity> imagList;
    List<LargeFragmentsEneity> lists;
    public Goodchoice_Fragment_Adapter(Context context,ImageView imageView,List<CyclicGraphEneity> list){
        this.context = context;
        this.imageView = imageView;
        this.list = list;
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return R.layout.goodchoice_fragment_item1;
        }else {
            return R.layout.goodchoice_fragment_item2;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //获取item布局
        View view = LayoutInflater.from(context).inflate(viewType, parent, false);
        if (viewType==R.layout.goodchoice_fragment_item1){
            return new Goodcholce_Item1(view);
        }else {
            return new Goodcholce_Item2(view);
        }
    }

    //控件赋值的相关操作
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //// TODO: 17-2-13 跳转到购物车界面
                Intent intent = new Intent(context, Login_Activity.class);
                context.startActivity(intent);
            }
        });

        if (holder instanceof Goodcholce_Item1){
            imagList = list.get(position).getImagList();
            Goodchoice_RollPager_Adapter adapter = new Goodchoice_RollPager_Adapter(imagList, context);
            ((Goodcholce_Item1) holder).rollPagerView.setPlayDelay(3000);//设置循环时间
            ((Goodcholce_Item1) holder).rollPagerView.setAdapter(adapter);//设置适配器
            //设置小圆点
            ((Goodcholce_Item1) holder).rollPagerView.setHintView(new ColorPointHintView(context, Color.YELLOW, Color.WHITE));

            //点击关闭小提示
            ((Goodcholce_Item1) holder).imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((Goodcholce_Item1) holder).linearLayout.setVisibility(View.GONE);
                }
            });
        }else {
//            lists = list.get(position).getAllitem();
            if (position==list.size()-1){
                lists = list.get(position).getList();
            }else {
                lists = list.get(position+1).getList();
            }
            //给第二个布局的控件赋值
            if (lists.size()!=0){
                ((Goodcholce_Item2) holder).tv1.setText(lists.get(0).getTitle());
                ((Goodcholce_Item2) holder).tv2.setText(lists.get(0).getChannel_title());
                UniversalImage.image(context,lists.get(0).getNew_cover_image_url(),((Goodcholce_Item2) holder).imageView);

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //// TODO: 17-2-13 跳转到商品详情界面
                    }
                });
                //设置并绑定布局管理器
                LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
                ((Goodcholce_Item2)holder).recyclerView.setLayoutManager(manager);

                //lists.get(position).getAllitem()
               Goodchoice_Small_Fragment_Adapter adapter = new Goodchoice_Small_Fragment_Adapter(context, lists.get(0).getList());
                ((Goodcholce_Item2)holder).recyclerView.setAdapter(adapter);
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //查找控件
    public class Goodcholce_Item1 extends RecyclerView.ViewHolder {

        LinearLayout linearLayout;
        RollPagerView rollPagerView;
        ImageView imageView;
        public Goodcholce_Item1(View itemView) {
            super(itemView);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.goodChoice_item1_linearLayout);
            rollPagerView = (RollPagerView) itemView.findViewById(R.id.goodChoice_item1_RollPageView);
            imageView = (ImageView) itemView.findViewById(R.id.goodChoice_item1_imageView);
        }
    }
    public class Goodcholce_Item2 extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView tv1,tv2;
        RecyclerView recyclerView;
        public Goodcholce_Item2(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.goodChoice_item2_imageView);
            tv1 = (TextView) itemView.findViewById(R.id.goodChoice_item2_textView);
            tv2 = (TextView) itemView.findViewById(R.id.goodChoice_item2_textView1);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.goodChoice_item2_recyclerView);
        }
    }
}
